import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import javax.sound.sampled.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

/*
 * 测试play class
 * */
public class TestPlay {

    private static Clip clip;

    private static String[] files = null;

    private static ArrayList<String> fileArray = new ArrayList<>();

    public static void main(String[] args) throws FileNotFoundException, JavaLayerException, InterruptedException {

        /*
        *         String media = "./audio";

        JukeCopy jukeCopy = new JukeCopy(media);
        jukeCopy.num = 1;
        jukeCopy.start();
        jukeCopy.stop();
        * */

/*        // 使用clip
        TestPlay testPlay = new TestPlay();

        // file path
        // String mediaPath = "./audio";
        String mediaPath = "C:\\Users\\22484\\Downloads\\JavaSoundDemo\\audio\\1-welcome.wav";

        testPlay.init(mediaPath);

        testPlay.play();
* */

        /*
        * 扫描文件
        *         String mediaPath = "D:\\Project";

        ArrayList<String> strings = loadJuke(mediaPath);
        for (int i = 0; i < strings.size(); i++) {
            System.out.println(strings.get(i));
        }
        * */


        String mediaPath = "C:\\Users\\22484\\Downloads\\JavaSoundDemo\\audio\\1-welcome.wav";
        FileInputStream fileInputStream = new FileInputStream(new File(mediaPath));

        Player player = new Player(fileInputStream);

        Thread thread = new Thread("fileInputStream"){
            @Override
            public void run() {
                try {
                    player.play();
                } catch (JavaLayerException e) {
                    throw new RuntimeException(e);
                }
            }
        };

        thread.start();

        // 模拟暂停
        System.out.println("delay 暂停3S");
        TimeUnit.SECONDS.sleep(3);

        thread.wait();

        System.out.println("进入暂停3S后唤醒");
        TimeUnit.SECONDS.sleep(3);

        thread.notify();
        System.out.println("唤醒成功3S后关闭");
        TimeUnit.SECONDS.sleep(3);

        player.close();
        thread.interrupt();

    }

    public void play() {
        clip.start();
    }

    public void init(String path) {

        /*
        * File absoluteFile = new File(path).getAbsoluteFile();

        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(absoluteFile);

        clip = AudioSystem.getClip();

        clip.open(audioInputStream);

        clip.loop(Clip.LOOP_CONTINUOUSLY);
        * */

    }

    // test 文件扫描
    public static ArrayList<String> loadJuke(String name) {
        File file = new File(name);
        // 判断是否文件夹和空文件
        if (file != null && file.isDirectory()) {
            files = file.list();

            for (int i = 0; i < files.length; i++) {
                // 判断后缀 fileArray.add(files[i]);
                if (files[i].endsWith(".au") ||
                        files[i].endsWith(".rmf") ||
                        files[i].endsWith(".mid") ||
                        files[i].endsWith(".wav") ||
                        files[i].endsWith(".aif") ||
                        files[i].endsWith(".aiff")) {
                    fileArray.add(file.getAbsolutePath() + "=" + files[i]);
                }
                File leafFile = new File(file.getAbsolutePath(), files[i]);
                if (leafFile.isDirectory()) {

                    String[] list = leafFile.list();
                    for (int i1 = 0; i1 < list.length; i1++) {
                        // 判断后缀 fileArray.add(list[i1]);
                        if (list[i1].endsWith(".au") ||
                                list[i1].endsWith(".rmf") ||
                                list[i1].endsWith(".mid") ||
                                list[i1].endsWith(".wav") ||
                                list[i1].endsWith(".aif") ||
                                list[i1].endsWith(".aiff")) {
                            fileArray.add(leafFile.getAbsolutePath() + "=" + list[i1]);
                        }
                    }
                }
            }
        }
        return fileArray;
    }
}
